﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AvatarHelper;
using Avatar.Model;
namespace Avatar.Management
{
    public static class SiteInformationManager
    {
        public static void Save(
            int _id,
            string _description,
            string _email,
            string _keyWord,
            string _siteName,
            string _title,
            string _url,
            string _logoUrl,
            string _footer,
            string _Statement,
            string _ServiceCenter,
            string _ContactUs,
            string _Charges,
            string _About
            )
        {
            #region 入口检测

            if (string.IsNullOrEmpty(_email)) throw new Exception("Email不能为空");
            if (string.IsNullOrEmpty(_siteName)) throw new Exception("网站名称不能为空");
            if (string.IsNullOrEmpty(_title)) throw new Exception("网站标题不能为空");
            if (string.IsNullOrEmpty(_url)) throw new Exception("网址不能为空");
            if (string.IsNullOrEmpty(_footer)) throw new Exception("Footer内容不能为空");
            
            if (!string.IsNullOrEmpty(_description) && _description.Length > 200) throw new Exception("网站描述不能超过200字符");
            if (!string.IsNullOrEmpty(_keyWord) && _keyWord.Length > 150) throw new Exception("关键字不能超过150字符");
            if (!string.IsNullOrEmpty(_footer) && _footer.Length > 3000) throw new Exception("Footer内容不能超过3000字符");
            if (!string.IsNullOrEmpty(_Statement) && _Statement.Length > 3000) throw new Exception("网站声明内容不能超过300字符");
            if (!string.IsNullOrEmpty(_ServiceCenter) && _ServiceCenter.Length > 3000) throw new Exception("服务中心内容不能超过300字符");
            if (!string.IsNullOrEmpty(_ContactUs) && _ContactUs.Length > 3000) throw new Exception("联系我们内容不能超过300字符");
            if (!string.IsNullOrEmpty(_Charges) && _Charges.Length > 3000) throw new Exception("收费标准内容不能超过300字符");
            if (!string.IsNullOrEmpty(_About) && _About.Length > 3000) throw new Exception("关于我们内容不能超过300字符");

            #endregion

            using (var _adc = DBHelper.GetDataContext())
            {
                try
                {
                    var _sc = _adc.SiteConfig.FirstOrDefault();
                    if (_sc == null)
                    {
                        _sc.Description = _description;
                        _sc.Email = _email;
                        _sc.KeyWord = _keyWord;
                        _sc.SiteName = _siteName;
                        _sc.Title = _title;
                        _sc.Url = _url;
                        _sc.LogoUrl = _logoUrl;
                        _sc.Footer = _footer;
                        _sc.About = _About;
                        _sc.Charges = _Charges;
                        _sc.ContactUs = _ContactUs;
                        _sc.Statement = _Statement;
                        _sc.ServiceCenter = _ServiceCenter;

                        _adc.SiteConfig.InsertOnSubmit(_sc);
                    }
                    else
                    {
                        _sc.Description = _description;
                        _sc.Email = _email;
                        _sc.KeyWord = _keyWord;
                        _sc.SiteName = _siteName;
                        _sc.Title = _title;
                        _sc.Url = _url;
                        _sc.LogoUrl = _logoUrl;
                        _sc.Footer = _footer;
                        _sc.About = _About;
                        _sc.Charges = _Charges;
                        _sc.ContactUs = _ContactUs;
                        _sc.Statement = _Statement;
                        _sc.ServiceCenter = _ServiceCenter;
                    }

                    _adc.SubmitChanges();
                }
                catch (Exception _e)
                {
                    throw new Exception("保存网站基本信息出错:" + _e.Message);
                }
            }
        }

        /// <summary>
        /// 获取网站基本信息
        /// </summary>
        /// <returns></returns>
        public static SiteConfig GetSiteInformation()
        {
            return DBHelper.GetAll<SiteConfig>("获取网站资料出错:").FirstOrDefault();
        }
    }
}
